import { Block, View, Image, Button } from '@tarojs/components'
import Taro from '@tarojs/taro'
import withWeapp from '@tarojs/with-weapp'
import { USER_INFO_KEY } from '../../common/constants.js'
import './auth.scss'
const app = Taro.getApp()

@withWeapp('Page')
class _C extends Taro.Component {
  state = {
    angle: 0
  }
  componentDidMount = () => {
    var _this = this
    Taro.onAccelerometerChange(function(res) {
      var angle = -(res.x * 30).toFixed(1)
      if (angle > 14) {
        angle = 14
      } else if (angle < -14) {
        angle = -14
      }
      if (_this.data.angle !== angle) {
        _this.setData({
          angle: angle
        })
      }
    })
  }
  go2Index = () => {
    console.log('go2Index')
    Taro.switchTab({
      url: '/pages/index/index'
    })
  }
  regain = () => {
    // const that = this
    // Taro.login({
    //   success: async function(res) {
    //     if (res.code) {
    //       //授权并保存用户信息
    //       const result = await wxRequest(
    //         '/center/login/' + res.code,
    //         'get',
    //         null,
    //         false,
    //         false
    //       )
    //       if (result.data != null) {
    //         app.globalData.user = result.data
    //         //同时保存于storge
    //         Taro.setStorageSync(USER_INFO_KEY, result.data)
    //         that.go2Index()
    //       }
    //     } else {
    //       console.log('授权失败！' + res.errMsg)
    //     }
    //   }
    // })
  }
  componentWillMount = () => {
    const that = this
    Taro.getStorage({
      key: USER_INFO_KEY,
      success: function(res) {
        if (res.data != null) {
          app.globalData.user = res.data
          that.go2Index()
        }
      },
      fail: async function(res) {
        // that.regain();
      }
    })
  }
  auth = e => {
    // const that = this
    // Taro.login({
    //   success: async function(res) {
    //     if (res.code) {
    //       const data = {
    //         nickName: e.detail.userInfo.nickName,
    //         avatarUrl: e.detail.userInfo.avatarUrl,
    //         sex: e.detail.userInfo.gender,
    //         code: res.code
    //         //授权并保存用户信息
    //       }
    //       const result = await wxRequest(
    //         '/center/auth',
    //         'post',
    //         data,
    //         true,
    //         true
    //       )
    //       //同时保存于storge
    //       Taro.setStorageSync(USER_INFO_KEY, result.data)
    //       that.regain()
    //     } else {
    //       console.log('授权失败！' + res.errMsg)
    //     }
    //   }
    // })
  }
  config = {
    navigationBarTitleText: '',
    enablePullDownRefresh: false,
    disableScroll: true
  }

  render() {
    const { angle } = this.state
    return (
      <View className="container">
        <Image className="title" src={require('./font-logo.png')}></Image>
        <View className="content">
          <View className="hd" style={'transform:rotateZ(' + angle + 'deg);'}>
            <Image className="logo" src={require('./logo.png')}></Image>
            <Image
              className="wave"
              src={require('./wave.png')}
              mode="aspectFill"
            ></Image>
            <Image
              className="wave wave-bg"
              src={require('./wave.png')}
              mode="aspectFill"
            ></Image>
          </View>
          <View className="bd">
            <View className="introduce">
              面向高校学生、老师、以及在职中的校友，以专业为基础，针对性地给在校大学生传授知识，职业方向指引，学习资源共享、学习规划。
            </View>
            {/*  <view class="confirm-btn" bindtap='go2Index'>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           <text>授权进入</text>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         </view>  */}
            <Button
              className="confirm-btn"
              openType="getUserInfo"
              onGetuserinfo={this.auth}
            >
              开启知识分享之旅
            </Button>
          </View>
        </View>
      </View>
    )
  }
}

export default _C
